<?php


namespace app\agent\controller;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\orders\Shopping as ShoppintCar;
use app\common\controller\Agent as Agents;
use app\common\model\order\Order;
use app\common\model\Warehouse;
use think\Session;

class Agent extends Agents
{
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Order();
        $daifukuan = $this->model->where(['pay'=>3,'sorter'=>1,'dep_id'=>$this->auth->id])->count();
        $daifahuo = $this->model->where(['deliverystatus'=>0,'pay'=>1,'sorter'=>1,'dep_id'=>$this->auth->id])->count();
        $daishouhuo = $this->model->where(['deliverystatus'=>1,'sorter'=>1,'dep_id'=>$this->auth->id])->count();
        $yiwancheng = $this->model->where(['deliverystatus'=>3,'sorter'=>1,'dep_id'=>$this->auth->id])->count();
        $total = $this->model->where(['sorter'=>1,'dep_id'=>$this->auth->id])->count();
        $this->view->assign('total',$total);
        $this->view->assign('status',['daifukuan'=>$daifukuan,'daifahuo'=>$daifahuo,'daishouhuo'=>$daishouhuo,'yiwancheng'=>$yiwancheng]);
        
        $this->view->assign('shoppingCar',ShoppintCar::shoppingCart($this->auth->id,1));
    }

    public function index(){
        $this->view->assign('dept',Session::get('deptone'));
        return $this->view->fetch();
    }
    public function order()
    {
        if ($this->request->isAjax())
        {
            $this->searchFields = 'id';
            list($where, $sort, $order, $offset,$limit) = $this->buildparams();
            $whereArr  = ['dep_id'=>$this->auth->id,'sorter'=>1];
            $total = $this->model
                ->with('orderItem')
                ->where($where)
                ->where($whereArr)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('orderItem')
                ->where($where)
                ->where($whereArr)
                ->order($sort, $order)
                ->limit($offset,$limit)
                ->select();
      
                $result = array("total" => $total, "rows" => $list);
                return json($result);
        }
        $this->view->assign('list','');
        $whereStatus = json_decode($filter = $this->request->get("filter", ''),true);
        $this->view->assign('whereStatus',$whereStatus);
        return $this->view->fetch();
    }

    /**
     * 删除收货地址
     * @throws \think\exception\DbException
     */
    public function delAddress($id)
    {
        if(!$row = Warehouse::destroy($id))
        {
            $this->error('操作失败！。');
        }
        $this->success('操作成功。','',$id);
    }

    /**
     * 收货地址列表
     * @throws \think\exception\DbException
     */
    public function addressList()
    {
        if(!$list = Warehouse::all())
        {
            $this->error('请添加收货地址');
        }
        $this->success('操作成功','',$list);
    }

    /**
     * 修改默认收货地址
     * @param $id
     * @throws \think\exception\DbException
     */
    public function upAddress($id)
    {
        if(!$row = Warehouse::get($id)) $this->error('参数错误，请选择正确的ID');
        if($row->status == 1) $this->error('无需修改！');
        Warehouse::update(['status'=>0],['agent_id'=>$this->auth->id,'sorter'=>1]);
        $row->status = 1;
        if(!$row->save()) $this->error('默认收货地址修改错误！');
        $this->success('默认收货地址修改成功！');
    }

    /**
     * 添加收货地址
     */
    public function addAddress()
    {
       if($this->request->isPost())
       {
           $url = Session::get('return_url_'.$this->auth->id);
         
           $data = $this->request->request('row/a');
           $data['agent_id'] = $this->auth->id;
           $data['sorter'] = 1;
           if($data['status'] == 1)
           {
               Warehouse::update(['status'=>0],['agent_id'=>$this->auth->id,'sorter'=>1]);
           }
           if(!$resultSet = Warehouse::create($data))
           {
               $this->error('添加收货地址错误！');
           }
         
           $this->success('收货地址添加成功！',$url,'','3');
       }

       Session::set('return_url_'.$this->auth->id,$this->request->request('url'));
       return $this->view->fetch();
    }

    /**
     * 添加收货地址
     */
    public function editAddress($id)
    {

        $row = Warehouse::get($id);
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }

    public function updateAddress()
    {
        if($this->request->isPost())
        {
            $data = $this->request->request('row/a');
            $data['sorter'] = 1;
            if($data['status'] == 1)
            {
                Warehouse::update(['status'=>0],['agent_id'=>$this->auth->id,'sorter'=>1]);
            }
            if(!$resultSet = Warehouse::update($data))
            {
                $this->error('修改收货地址错误！');
            }
            $this->success('收货地址修改成功！',$_SERVER['HTTP_REFERER']);
        }
    }

    public function myAddress()
    {
        $list = Warehouse::where(['agent_id'=>$this->auth->id,'sorter'=>1])->select();
        $this->view->assign('list',$list);
        return $this->view->fetch();
    }

    public function deliverystatus($ids)
    {
        $row = Order::get($ids);
        $row->deliverystatus = 3;
        if (!$row->save())
        {
            return json(['msg'=>'成功！','status'=>0]);
        }
        return json(['msg'=>'失败！','status'=>1]);
    }

}